//
//  APIWeek.m
//  Bracelet
//
//  Created by jing zhao on 2/15/15.
//  Copyright (c) 2015 jing zhao. All rights reserved.
//

#import "APIWeekTotal.h"
#import "FileReadAndWrite.h"
#define FILENAMECACHE_APIWORKOUTWEEKTOTAL @"fileName_apiWorkoutWeekTotal"

@implementation APIWeekTotal
-(instancetype) initFromTodayData:(NSDictionary*)dic
{
    self = [super init];
    if (self) {
        self.strLast_update_time = [dic objectForKey:APIKEYLAST_UPDATE_TIME];
        self.strPercent = [dic objectForKey:APIKEYPERCENT];
        self.strRankResult = [dic objectForKey:APIKEYRANKRESULT];
        self.color = [self getColorWithString:self.strRankResult];
        self.strRemainDays = [dic objectForKey:APIKEYREMAINDAYS];
        self.strRunTotal_metre = [dic objectForKey:APIKEYRUNTOTAL_METRE];
        self.strTotalMoney = [dic objectForKey:APIKEYTOTAL_MONEY];
        self.strTotalTarget = [dic objectForKey:APIKEYTOTALTARGET];
        self.strHtml = [dic objectForKey:APIKEYHTML];
    }
    return self;
}


+(instancetype)apiTodayWithData:(NSDictionary*)dic
{
    return [[self alloc]initFromTodayData:dic];
}



+(void)setApiRelativePosition:(NSString*)relativePostion
                   getTheWeek:(void (^)(APIWeekTotal *week)) successBlock
                      failure:(void (^)( NSError *error)) failureBlock
{
    
    NSString *strUserId = [[NSUserDefaults standardUserDefaults]objectForKey:USERID];
    NSDictionary *headerData =@{@"BasicAuthUsername":strUserId};
    NSDictionary *data = @{@"flag":@"week",@"ios_android_flag":@"ios"};
  
    [[APIConnection shareInstance]post:relativePostion data:data header:headerData netError:^(NSError *error) {
        failureBlock(error);
        
        NSDictionary *dicCanch = [FileReadAndWrite getTheStorageDataWithFile:FILENAMECACHE_APIWORKOUTWEEKTOTAL];
        if (dicCanch) {
            NSDictionary *dicSuccess = [dicCanch objectForKey:APIKEYData];
            successBlock([APIWeekTotal apiTodayWithData:dicSuccess]);
        }
    } apiFinish:^(id data) {
        NSDictionary *dictionaryTemp = data;
        NSInteger codeTemp= [[dictionaryTemp objectForKey:APIKEYCODE]integerValue];
        if (codeTemp ==0) {
            NSDictionary *dicSuccess = [data objectForKey:APIKEYData];
            successBlock([APIWeekTotal apiTodayWithData:dicSuccess]);
            
            //存入缓存
            [FileReadAndWrite storageDataWithFile:FILENAMECACHE_APIWORKOUTWEEKTOTAL andData:dictionaryTemp];
        }
        else
        {
            failureBlock(nil);
        }
    }];
}



-(UIColor*)getColorWithString:(NSString*)strRankResult
{
    UIColor *colorTemp=[UIColor colorWithRed:191.0/255.0 green:190.0/255.0 blue:218.0/255.0 alpha:1.0];
    if ([strRankResult isEqualToString:@"Poor"]) {
    }
    else if ([strRankResult isEqualToString:@"Good"])
    {
        colorTemp =[UIColor colorWithRed:117.0/255.0 green:194.0/255.0 blue:189.0/255.0 alpha:1.0];
    }
    else if ([strRankResult isEqualToString:@"Very good"])
    {
        colorTemp =[UIColor colorWithRed:163.0/255.0 green:217.0/255.0 blue:99.0/255.0 alpha:1.0];
    }
    else if ([strRankResult isEqualToString:@"Excellent"])
    {
        colorTemp =[UIColor colorWithRed:230.0/255.0 green:176.0/255.0 blue:18.0/255.0 alpha:1.0];
    }
    else if ([strRankResult isEqualToString:@"Unbelievable"])
    {
        colorTemp =[UIColor colorWithRed:219.0/255.0 green:0.0/255.0 blue:17.0/255.0 alpha:1.0];
    }
    else if ([strRankResult isEqualToString:@"Unbeatable"])
    {
        colorTemp =[UIColor colorWithRed:0.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:1.0];
    }
    return colorTemp;
}

@end
